//
//  XDMyInvestmentViewController.m
//  xindailianmeng
//
//  Created by 张东 on 2017/2/8.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDMyInvestmentViewController.h"
#import "UIButton+Extension.h"
#import "XDMyInvestmentCell.h"
#import "AFHTTPSessionManager+POST.h"
#import "XDMyInvestmentModel.h"
#import "MJExtension.h"
#import "XDProductsIntroduceController.h"
#import "ZHRTRequestFailureShowViewOrDetail.h"
@interface XDMyInvestmentViewController ()<UITableViewDelegate,UITableViewDataSource>
//点击title弹出框的背景view
@property (nonatomic, weak) UIView *titleCoverView;

@property (nonatomic, weak) UIView *selectView;
////title弹出框的背景view上button的tag
//@property (nonatomic,assign) NSInteger tag;
@property (nonatomic, weak) UIButton *titleBtn;
//模型数组
@property (nonatomic, strong) NSArray *MyInvestmentArr;


@property (nonatomic, weak) UITableView *table;


@property (nonatomic, weak) UIView * errorView;
@property (nonatomic, weak) UIView *progressHUDView;
@end

@implementation XDMyInvestmentViewController
- (void)loadData {
     [ZHRTRequestFailureShowViewOrDetail showProgressHUDWithInView:self.progressHUDView type:0];
    NSDictionary * param = @{@"method":@"myInvestment",@"postType":@"all",@"status":@"ing",@"page":@"0"};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {

        [self.progressHUDView removeFromSuperview];

        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
            
            [self.errorView removeFromSuperview];
            self.MyInvestmentArr = [XDMyInvestmentModel mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"myInvestmentList"]];
            [self.table reloadData];
        } else if ([responseObject[@"code"] isEqualToString:@"100001"]){ // 超时
            
            
        } else {
              [ZHRTRequestFailureShowViewOrDetail showViewDetail:responseObject[@"message"] view:self.view];
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
        if (!_errorView) {
            
            [self.view addSubview:self.errorView ];
            
            [ZHRTRequestFailureShowViewOrDetail showRequestFailureForView:_errorView target:self action:@selector(loadData) type:0];
        }

         [self.progressHUDView removeFromSuperview];
    }];
    
}

- (NSArray *)MyInvestmentArr {
    if (_MyInvestmentArr == nil) {
        _MyInvestmentArr = [NSArray array];
    }
    return _MyInvestmentArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView];
    [self setupUI];
    [self loadData];
}

- (void)setupUI {
    
    [self setupTableViewHeaderView];
    
    UITableView *tablew = [[UITableView alloc] initWithFrame:CGRectMake(0, 53, kScreenW, kScreenH - 53 - 64) style:UITableViewStyleGrouped];
    tablew.delegate = self;
    tablew.dataSource = self;
    tablew.showsVerticalScrollIndicator = NO;
    tablew.showsHorizontalScrollIndicator = NO;
    self.table = tablew;
    [self.view addSubview:tablew];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 30)];
    tablew.tableFooterView = view;
  
}

- (void)setupTableViewHeaderView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 53)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    UIButton *investCenterBtn = [UIButton buttonWithFrame:CGRectMake(0, 0, kScreenW * 0.5, 53) norImgNamed:nil selImgNamed:nil title:@"投资中" titleColor:kColorRGB(153,153,153) selColor:kColorRGB(187,147,115) titleFont:16 target:self selector:@selector(progressBtnClick:)];
    investCenterBtn.tag = 200;
    investCenterBtn.selected = YES;
    [view addSubview:investCenterBtn];
    
    UIButton *finishBtn = [UIButton buttonWithFrame:CGRectMake(kScreenW * 0.5, 0, kScreenW * 0.5, 53) norImgNamed:nil selImgNamed:nil title:@"已完成" titleColor:kColorRGB(153,153,153) selColor:kColorRGB(187,147,115) titleFont:16 target:self selector:@selector(progressBtnClick:)];
    finishBtn.tag = 201;
    [view addSubview:finishBtn];
    
    UIView *carveView = [[UIView alloc] initWithFrame:CGRectMake(kScreenW * 0.5, 12, 1, 28)];
    carveView.backgroundColor = kColorRGB(238,238,238);
    [view addSubview:carveView];
    
    UIView *carveView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 52.5, kScreenW, 0.5)];
    carveView1.backgroundColor = kColorRGBA(0, 0, 0, 0.2);
    [view addSubview:carveView1];
}

- (void)setupTitleView {
    UIButton *titleBtn = [UIButton buttonWithFrame:CGRectMake(0, 0, 200, 44) norImgNamed:@"draw" selImgNamed:nil title:@"全部" titleColor:kColorRGB(187,147,115) selColor:nil titleFont:19 target:self selector:@selector(titleBtnClick:)];
    titleBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
    titleBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 84, 0, 0);
    titleBtn.selected = YES;
    self.titleBtn = titleBtn;
    self.navigationItem.titleView = titleBtn;
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)titleBtnClick: (UIButton *)titleBtn {
    
    if (titleBtn.selected) {
        self.titleCoverView.frame = kScreenB;
        [UIView animateWithDuration:0.5 animations:^{
         self.selectView.frame = CGRectMake(0, 0, kScreenW, 140);
        } completion:^(BOOL finished) {
            
        }];
        titleBtn.selected = NO;
    } else {
        [UIView animateWithDuration:0.5 animations:^{
             self.titleCoverView.backgroundColor = kColorRGBA(0, 0, 0, 0);
             self.selectView.frame = CGRectMake(0, - 200, kScreenW, 140);
        } completion:^(BOOL finished) {
            [self.titleCoverView removeFromSuperview];
            [self.selectView removeFromSuperview];
        }];
        titleBtn.selected = YES;
    }
}

- (UIView *)titleCoverView {
    if (_titleCoverView == nil) {
        UIView *titleCoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
        titleCoverView.backgroundColor = kColorRGBA(0, 0, 0, 0.5);
        _titleCoverView = titleCoverView;
        [self.view addSubview:titleCoverView];
     
    }
  
    return _titleCoverView;
}


- (UIView *)selectView {
    
    if (_selectView == nil) {
        
        UIView *selectView = [[UIView alloc] initWithFrame:CGRectMake(0, - 200, kScreenW, 140)];
        selectView.backgroundColor = kColorRGB(255, 255, 255);
        [self.view addSubview:selectView];
        self.selectView = selectView;
        UIButton *allBtn = [UIButton buttonWithFrame:CGRectMake(28, 32, (kScreenW - 56 - 22) / 3, 33) norBagImgNamed:@"greyBtn" selBagImgNamed:@"yellowBtn" title:@"全部" titleColor:kColorRGB(0, 0, 0) selColor:kColorRGB(187,147,115) titleFont:14  target:self selector:@selector(allSelectBtnClicK:)];
        allBtn.adjustsImageWhenHighlighted = NO;
        allBtn.tag = 300;
        [allBtn setBackgroundImage:[UIImage imageNamed:@"yellowBtn"] forState:UIControlStateDisabled];
        [selectView addSubview:allBtn];
        
        UIButton *planPostBtn = [UIButton buttonWithFrame:CGRectMake(CGRectGetMaxX(allBtn.frame) + 11, 32, (kScreenW - 56 - 22) / 3, 33) norBagImgNamed:@"greyBtn" selBagImgNamed:@"yellowBtn" title:@"优选计划" titleColor:kColorRGB(0, 0, 0) selColor:kColorRGB(187,147,115) titleFont:14  target:self selector:@selector(allSelectBtnClicK:)];
        planPostBtn.adjustsImageWhenHighlighted = NO;
        planPostBtn.tag = 301;
        [selectView addSubview:planPostBtn];
        
        UIButton *sanPostBtn = [UIButton buttonWithFrame:CGRectMake(CGRectGetMaxX(planPostBtn.frame) + 11, 32, (kScreenW - 56 - 22) / 3, 33) norBagImgNamed:@"greyBtn" selBagImgNamed:@"yellowBtn" title:@"散标理财" titleColor:kColorRGB(0, 0, 0) selColor:kColorRGB(187,147,115) titleFont:14  target:self selector:@selector(allSelectBtnClicK:)];
        sanPostBtn.adjustsImageWhenHighlighted = NO;
        sanPostBtn.tag = 302;
        [selectView addSubview:sanPostBtn];
        
        UIButton *newBtn = [UIButton buttonWithFrame:CGRectMake(28, CGRectGetMaxY(allBtn.frame) + 11, (kScreenW - 56 - 22) / 3, 33) norBagImgNamed:@"greyBtn" selBagImgNamed:@"yellowBtn" title:@"新手专享" titleColor:kColorRGB(0, 0, 0) selColor:kColorRGB(187,147,115) titleFont:14  target:self selector:@selector(allSelectBtnClicK:)];
        newBtn.adjustsImageWhenHighlighted = NO;
        newBtn.tag = 303;
        [selectView addSubview:newBtn];
    }
    
    return _selectView;
    
}

- (void)allSelectBtnClicK: (UIButton *)btn {
    
    btn.selected = YES;
    [UIView animateWithDuration:0.5 animations:^{
        self.titleCoverView.backgroundColor = kColorRGBA(0, 0, 0, 0);
        self.selectView.frame = CGRectMake(0, - 200, kScreenW, 140);
    } completion:^(BOOL finished) {
        [self.titleCoverView removeFromSuperview];
        [self.selectView removeFromSuperview];
    }];
    self.titleBtn.selected = YES;
    
}

- (void)progressBtnClick: (UIButton *)btn {
    
    btn.selected = YES ;
    if (btn.tag == 200) {
        btn = [self.view viewWithTag:201];
        btn.selected = NO;
    } else {
        btn = [self.view viewWithTag:200];
        btn.selected = NO;
    }
}

#pragma mark -- table 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.MyInvestmentArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *myID = @"MyInvestmentID";
    XDMyInvestmentCell *cell = [tableView dequeueReusableCellWithIdentifier:myID];
    if (cell == nil) {
        cell = [[XDMyInvestmentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myID];
    }
    cell.model = self.MyInvestmentArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XDMyInvestmentModel *model = self.MyInvestmentArr[indexPath.row];
    XDProductsIntroduceController *vc = [[XDProductsIntroduceController alloc] init];
    vc.investId = model.investId;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 130;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}


- (UIView *)progressHUDView {
    if (_progressHUDView == nil) {
        UIView *view = [[UIView alloc] initWithFrame:kScreenB];
        _progressHUDView = view;
        [self.view addSubview:view];
    }
    return _progressHUDView;
}

- (UIView *)errorView {
    if (_errorView == nil) {
        UIView *view = [[UIView alloc] initWithFrame:kScreenB];
        _errorView = view;
        [self.view addSubview:view];
    }
    return _errorView;
}
@end
